package com.easondongh.system.controller;

import com.easondongh.system.constants.Constants;
import com.easondongh.system.domain.SysMenu;
import com.easondongh.system.domain.SysUser;
import com.easondongh.common.enums.ResponseEnum;
import com.easondongh.common.response.ResponseObj;
import com.easondongh.system.service.SysMenuService;
import com.easondongh.common.shiro.ShiroUtils;
import com.easondongh.system.utils.StringUtils;
import com.easondongh.system.vo.MenuTreeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author EasonDongH
 * @date 2020/4/29 15:14
 */
@Slf4j
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private SysMenuService menuService;

    /**
     * 根据当前登录用户的权限，获取菜单列表
     * 界面通过父子关系渲染
     * @return
     */
    @GetMapping("/loadMenu")
    @ResponseBody
    public ResponseObj<Map<String, MenuTreeNode>> loadMenu() {
        //得到当前登录的用户
        SysUser user = ShiroUtils.getCurrentLoginUser();
        List<MenuTreeNode> menuTreeNodes = null;
        if(Constants.SUPER_ADMIN.equals(user.getType())) {
            menuTreeNodes = this.menuService.queryAllMenuTreeNode();
        } else {
            menuTreeNodes = this.menuService.queryMenuTreeNodeByUserId(user.getId());
        }
        Map<String, MenuTreeNode> map = new LinkedHashMap<>();
        if(menuTreeNodes != null) {
            for (MenuTreeNode node : menuTreeNodes) {
                map.put(node.getTypeCode(), node);
            }
        }
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, map);
    }

    /**
     * 直接查询菜单列表，界面通过id、pid来渲染数学菜单
     * @return
     */
    @GetMapping("/loadMenuList")
    public ResponseObj<List<SysMenu>> loadMenuList() {
        List<SysMenu> menus = this.menuService.loadMenuList();
        return new ResponseObj<List<SysMenu>>(ResponseEnum.QUERY_SUCCESS, menus);
    }

    /**
     * 获取当前菜单的最大排序号
     * @return
     */
    @GetMapping("/getMaxOrderNum")
    public ResponseObj<Integer> getMaxOrderNum() {
        Integer orderNum = this.menuService.getMaxOrderNum();
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, orderNum);
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @PostMapping("/addMenu")
    public ResponseObj addMenu(@RequestBody SysMenu menu) {
        if(menu == null || StringUtils.isBlank(menu.getTitle())) {
            log.warn("添加菜单时标题不能为空");
            return new ResponseObj(ResponseEnum.ADD_FAIL);
        }
        if(menu.getType().equals(Constants.MENU_TYPE_TOP)) {
            menu.setPid(Constants.TOP_MENU_PID);
        }
        if(menu.getOrderNum() == null) {
            menu.setOrderNum(this.menuService.getMaxOrderNum() + 1);
        }
        this.menuService.save(menu);
        return new ResponseObj(ResponseEnum.ADD_SUCCESS);
    }

    /**
     * 根据id更新菜单
     * @param menu
     * @return
     */
    @PutMapping("/updateById")
    public ResponseObj updateById(@RequestBody SysMenu menu) {
        if(menu == null || menu.getId() == null) {
            log.warn("updateById修改菜单信息时菜单id不能为空");
            return ResponseObj.UpdateFail;
        }
        this.menuService.updateById(menu);
        return ResponseObj.UpdateSuccess;
    }

    /**
     * 排除id = #{id} 及 其所有子节点、孙子节点等
     * @param id
     * @return
     */
    @GetMapping("/getMenuExcludeById")
    public ResponseObj<List<SysMenu>> getMenuExcludeById(Integer id) {
        if(id == null) {
            return ResponseObj.QueryFail;
        }
        List<SysMenu> list = this.menuService.getMenuExcludeById(id);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, list);
    }

    /**
     * 根据id查询菜单
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public ResponseObj<SysMenu> getById(Integer id) {
        if(id == null) {
            return ResponseObj.QueryFail;
        }
        SysMenu menu = this.menuService.getById(id);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, menu);
    }

    /**
     * 统计该id下的子菜单数量
     * @param id
     * @return
     */
    @GetMapping("/countChildMenuById")
    public ResponseObj<Integer> countChildMenuById(Integer id) {
        if(id == null) {
            log.warn("统计子菜单数量时id不能为空");
            return ResponseObj.QueryFail;
        }
        Integer count = this.menuService.countChildMenuById(id);
        return new ResponseObj<Integer>(ResponseEnum.QUERY_SUCCESS, count);
    }

    /**
     * 根据id删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById")
    public ResponseObj deleteById(Integer id) {
        this.menuService.removeById(id);
        return ResponseObj.DeleteSuccess;
    }
}
